package it.univpm.diiga.memorysim.frames.disk;

import it.univpm.diiga.memorysim.data.DataPlugin;
import it.univpm.diiga.memorysim.kernel.shared.FileRequest;
import it.univpm.diiga.memorysim.kernel.shared.tree.MovingBox;

import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.window.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;


public class FileSequence extends ApplicationWindow {
	//private String[] operation=new String[]{"Creazione file","Creazione directory","Lettura","Scrittura","Cancellazione"};
	private String[] operation=new String[]{"Lettura","Scrittura","Cancellazione"};
	private Combo operationCombo;
	private Group group;
	private Table table;
	private InputDialog dlg,dlg1;
	private String Nome;
	private String Path="";  //  @jve:decl-index=0:
	private int Dimension;
	private boolean file;
	
  public FileSequence(TreeViewer viewer) {
    super(null);
    ISelection sel = viewer.getSelection();
    MovingBox element= (MovingBox)((IStructuredSelection) sel).getFirstElement();
    this.Nome=element.getName();
    if (Nome!="Disco"){
    this.Dimension=element.dimensione;
    String Father="";
    MovingBox LivSup= element;
    while(Father!="Disco"){
    	LivSup = LivSup.getParent();
    	Father=LivSup.getName();
    	String OldLine= Path;
    	Path=Father+"/"+OldLine;
    }
    if(element.getTitle()==null) file=true;
	else file=false;
    }else {
    	Path="Disco";
    }
  }

  public void run() {
    setBlockOnOpen(true);
    open();
  }

  /**
   * Configures the shell
   * 
   * @param shell the shell
   */
  protected void configureShell(Shell shell) {
    super.configureShell(shell);
    shell.setText("Operazione su file");
  }

  /**
   * Creates the main window's contents
   * 
   * @param parent the main window
   * @return Control
   */
  protected Control createContents(Composite parent) {
	GridData gridData = new GridData();
	gridData.verticalSpan = 7;
	gridData.verticalAlignment = GridData.FILL;
	gridData.widthHint = 350;
	gridData.heightHint = 216;
	GridData gridData1 = new GridData();
	gridData1.verticalSpan = 7;
	gridData1.verticalAlignment = GridData.FILL;
	gridData1.widthHint = 500;
	gridData1.heightHint = 260;
    Composite composite = new Composite(parent, SWT.NONE);
    GridLayout gridLayout = new GridLayout();
    gridLayout.numColumns = 2;
    composite.setLayout(gridLayout);

    final Label label1 = new Label(composite, SWT.NONE);
    label1.setText("Operazione sul file: ");
    final Label label2 = new Label(composite, SWT.NONE);
    label2.setText("File");
    
    operationCombo=new Combo(composite, SWT.NONE);
    operationCombo.setItems(operation);
    operationCombo.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
    Button show = new Button(composite, SWT.PUSH);
    show.setText("Inserisci operazione");
    show.addSelectionListener(new SelectionAdapter() {
		public void widgetSelected(SelectionEvent event) {
    		/*if(operationCombo.getSelectionIndex()==0){
    			dlg = new InputDialog(Display.getCurrent().getActiveShell(),
    					"Dimensione File", "Inserire la dimensione [Kb]", null,new Validator());
    			if (dlg.open() == Window.OK) {
    				dlg1 = new InputDialog(Display.getCurrent().getActiveShell(),
    						"Nome File", "Inserire il nome del file", null,new InputValidator());}
    			if (dlg1.open()==Window.OK) {
    				TableItem itemRow=new TableItem(table,SWT.NONE);
    				itemRow.setText(0, dlg1.getValue());
    				DataPlugin.hddused+=Integer.parseInt(dlg.getValue());
    				itemRow.setText(1, dlg.getValue());
    				
    				if(!file)itemRow.setText(2, Path+Nome+"/");
    				else itemRow.setText(2, Path);
    				itemRow.setText(3, operationCombo.getText());
    			}
    			
    		}
    		if(operationCombo.getSelectionIndex()==1){
    			dlg = new InputDialog(Display.getCurrent().getActiveShell(),
    					operationCombo.getText(), "Inserire il nome della cartella", null,new InputValidator());
    			if (dlg.open() == Window.OK) {
    				TableItem itemRow=new TableItem(table,SWT.NONE);
    				itemRow.setText(0, dlg.getValue());
    				itemRow.setText(1, "0");
    				if(!file)itemRow.setText(2, Path+Nome+"/");
    				else itemRow.setText(2, Path);
    				itemRow.setText(3, operationCombo.getText());
    			}
    		}*/
    		if(operationCombo.getSelectionIndex()==0){
    			TableItem itemRow=new TableItem(table,SWT.NONE);
    			itemRow.setText(0, Nome);
    			itemRow.setText(1, Dimension+"");
    			if(file) itemRow.setText(2, Path+Nome);
        		else itemRow.setText(2, Path+Nome);
    			itemRow.setText(3, operationCombo.getText());
    		}
    		if(operationCombo.getSelectionIndex()==1){
        		TableItem itemRow=new TableItem(table,SWT.NONE);
        		itemRow.setText(0, Nome);
        		itemRow.setText(1, Dimension+"");
        		if(file) itemRow.setText(2, Path+Nome);
        		else itemRow.setText(2, Path+Nome);
        		itemRow.setText(3, operationCombo.getText());
    		}
    		if(operationCombo.getSelectionIndex()==2){
        		TableItem itemRow=new TableItem(table,SWT.NONE);
        		itemRow.setText(0, Nome);
        		itemRow.setText(1, Dimension+"");
        		if(file) itemRow.setText(2, Path+Nome);
        		else itemRow.setText(2, Path+Nome);
        		itemRow.setText(3, operationCombo.getText());
    		}
    		updateListOperation();
      }
    });
    GridLayout gridLayout1 = new GridLayout();
	gridLayout1.numColumns = 2;
	group = new Group(composite, SWT.NONE);
	group.setText("Lista Operazioni");
	group.setLayoutData(gridData1);
	group.setLayout(gridLayout1);
    table = new Table(group, SWT.NONE|SWT.BORDER);
    table.setLayoutData(gridData);
    table.setHeaderVisible(true);
    String[] titles = { "Nome", "Dimensione", "Path                    ", "Operazione" };
    for (int loopIndex=0; loopIndex<titles.length; loopIndex++) {
    	TableColumn column = new TableColumn(table, SWT.NULL);
        column.setText(titles[loopIndex]);
        column.setResizable(true);
        
    }
    for (int loopIndex = 0; loopIndex < titles.length; loopIndex++) {
         table.getColumn(loopIndex).pack();
    }    
    //dati già caricati:
    for(int i=0;i<DataPlugin.getFileListRequest().size();i++){
    	FileRequest dato = DataPlugin.getFileListRequest().get(i);
    	TableItem itemRow=new TableItem(table,SWT.NONE);
		itemRow.setText(0, dato.getName());
		itemRow.setText(1, dato.getDim()+"");
		itemRow.setText(2, dato.getPath());
		
		if (dato.getTipoOperazione()==0)
			itemRow.setText(3, "Lettura"+"");
		else if(dato.getTipoOperazione()==1)
			itemRow.setText(3, "Scrittura"+"");
		else if(dato.getTipoOperazione()==2)
			itemRow.setText(3, "Creazione file"+"");
		else if(dato.getTipoOperazione()==3)
			itemRow.setText(3, "Cancellazione"+"");	
		
    }
    
    Button eliminaOperazione = new Button(group, SWT.PUSH);
    eliminaOperazione.setText("Elimina operazione");
    eliminaOperazione.addSelectionListener(new SelectionListener(){

		public void widgetDefaultSelected(SelectionEvent arg0) { }

		public void widgetSelected(SelectionEvent arg0) {
			table.remove(table.getSelectionIndices());
			updateListOperation();
		}
    	
    });
    parent.pack();
    return composite;
  }

  public void updateListOperation(){
		 DataPlugin.newFileListRequest();
		 for(int i=0;i<table.getItemCount();i++){
				TableItem itemEvent = table.getItem(i);
				String nome =itemEvent.getText(0);
				int dim=Integer.parseInt(itemEvent.getText(1));
				String path =itemEvent.getText(2);
				String op =itemEvent.getText(3);
				DataPlugin.addFileRequest(nome, dim, path, op);
			}
  }
  
}


class Validator implements IInputValidator {
	public String isValid(String newInt) {
		try{
			@SuppressWarnings("unused")
			int len = Integer.parseInt(newInt);
			return null;
	    }catch(Exception e1){
	    	return "Inserire un numero valido";
	    	}
	    }
}

class InputValidator implements IInputValidator {
	public String isValid(String newInt) {
		if(newInt=="") return "Nome file non valido";
		else return null;
	}
}	
